<template>
  <view class="content">
    <u-sticky :h5NavHeight="0">
      <!-- #ifndef MP-TOUTIAO -->
      <singleHeader pageTitle="商品评价"></singleHeader>
      <!-- #endif -->
      <pageHeader @search="search">
        <pageTime></pageTime>
      </pageHeader>
    </u-sticky>

    <pageList :items="list"></pageList>
    <toolsMore :status="moreStatus"></toolsMore>
  </view>
</template>

<script>
import singleHeader from "@/components/diySystemPage/components/singleHeader/index.vue";
import pageHeader from "./components/page-header.vue";
import pageList from "./components/page-list.vue";
import pageTime from "./components/page-time.vue";
import toolsMore from "@/components/tools/tools-more.vue";
import { orderCommentGoodsList } from "@/api/admin";

export default {
  components: {
    singleHeader,
    pageHeader,
    pageList,
    pageTime,
    toolsMore,
  },

  data() {
    return {
      date_range: {
        begin_time: "", //开始日期
        end_time: "", //结束日期
      },

      list: [],
      moreStatus: 1,
      page: 1,
      isEnd: false,
    };
  },

  onLoad() {
    this.$utils.hideShareMenu();
    this.clearData();
  },

  onShow() {
    this.Emit.$on("handleTime", this.handleTime);
  },

  onHide() {
    this.Emit.$off("handleTime");
  },

  methods: {
    handleTime(date_range) {
      this.date_range = date_range;
      this.clearData();
    },

    clearData() {
      this.page = 1;
      this.isEnd = false;
      this.list = [];

      this.orderCommentGoodsList();
    },

    orderCommentGoodsList() {
      let { list: oldList, page: pageNo, value, date_range } = this;

      this.moreStatus = 2;
      orderCommentGoodsList({
        pageNo,
        value,
        pageSize: 20,
        ...date_range,
      })
        .then((res) => {
          uni.stopPullDownRefresh();
          this.moreStatus = 1;

          if (res.code === 0) {
            let { list, totalCount } = res.data,
              count = parseInt(totalCount);

            if (list.length) {
              oldList.push(...list);
              this.page = this.page + 1;
            }

            if (oldList.length >= count) {
              this.isEnd = true;
              this.moreStatus = 3;
            }
          }
        })
        .catch(() => {
          this.moreStatus = 1;
          uni.stopPullDownRefresh();
        });
    },
  },

  onReachBottom() {
    if (this.isEnd) {
      return;
    }

    this.orderCommentGoodsList();
  },

  onPullDownRefresh() {
    this.clearData();
  },
};
</script>

<style lang="less" scoped>
.content {
  position: relative;
  background: #f8f8f8;
  min-height: 100vh;
  padding-bottom: env(safe-area-inset-bottom);
  box-sizing: border-box;
}
</style>
